package com.zahb.qadx.ui.activity;

import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.zahb.qadx.R;
import com.zahb.qadx.base.BaseActivity;
import com.zahb.qadx.base.BaseApplication;
import com.zahb.qadx.net.RetrofitService;
import com.zahb.qadx.ui.fragment.TrainRecommendListFragment;
import com.zahb.qadx.ui.fragment.TrainTaskListFragment;
import com.zahb.qadx.util.Constant;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.android.schedulers.AndroidSchedulers;

public class OldTrainDetailsActivity extends BaseActivity {

    @BindView(R.id.tv_train_classname)
    AppCompatTextView train_classname;
    @BindView(R.id.tv_train_introduction)
    AppCompatTextView train_introduction;
    @BindView(R.id.tv_train_time)
    AppCompatTextView train_time;

    @BindView(R.id.tv_train_status)
    AppCompatTextView train_status;
    @BindView(R.id.tab_layout_train)
    TabLayout mTabLayout;
    @BindView(R.id.view_pager2_train)
    ViewPager2 mViewPager2;

    private String[] mTitles = new String[]{"学习任务", "推荐学习"};
    private Fragment[] mFragments = new Fragment[mTitles.length];

    private int classid;
    @Override
    protected int getLayout() {
        return R.layout.activity_train_details_old;
    }

    @Override
    protected void initViews() {
        mUnbinder = ButterKnife.bind(this);
        classid = getIntent().getIntExtra("id",0);
        //BToast.showText(TrainDetailsActivity.this, "点击了"+classid);

        TrainTaskListFragment trainTaskListFragment = new TrainTaskListFragment();
        trainTaskListFragment.classid = classid;
        mFragments[0] = trainTaskListFragment;

        TrainRecommendListFragment trainRecommendListFragment = new TrainRecommendListFragment();
        trainRecommendListFragment.classid = classid;
        mFragments[1] = trainRecommendListFragment;

        mViewPager2.setAdapter(new TrainListFragmentAdapter(this,mFragments));
        mViewPager2.setOffscreenPageLimit(mFragments.length - 1);
        TabLayoutMediator mediator = new TabLayoutMediator(mTabLayout, mViewPager2,
                (tab, position) -> tab.setText(mTitles[position]));
        mediator.attach();
        getTrainDetailsHead();
        /*getTrainDetailsList();*/
    }

    @Override
    protected void onResume() {
        super.onResume();
//        getTrainDetailsHead();

    }

    @Override
    protected void onStart() {
        super.onStart();
        //getTrainDetailsList();
        Log.e("走了这里","onStart");
    }

    //培训头信息
    private void getTrainDetailsHead(){
        if (BaseApplication.getContext().getDataLogin() == null) return;
        addDisposable(RetrofitService.getNetService()
                .getTrainDetailsHead(classid, Constant.TRAIN_APP_KEY, BaseApplication.getContext().getDataLogin().getCurrentOrg(),
                        BaseApplication.getContext().getDataLogin().getUser().getId())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(response -> {
                    if (response.getStatusCode() == Constant.CODE_SUCCESS) {
                        String clssname = response.getData().getClassName();
                        train_classname.setText(clssname);
                        String briefIntroduction = response.getData().getBriefIntroduction();
                        train_introduction.setText(briefIntroduction.equals("") ? "暂无简介" : briefIntroduction);
                        String startTime = response.getData().getStartTime();
                        String endTime = response.getData().getEndTime();
                        train_time.setText("有效期：" + splittime(startTime) + " ～ " + splittime(endTime)
                                +"\n总学时："+response.getData().getClassHoursStr());
                        int way = response.getData().getWay();
                        if (way == 1) {
                            train_status.setText("线上");
                        } else if (way == 2) {
                            train_status.setText("面授");
                        } else if (way == 0) {
                            train_status.setVisibility(View.GONE);
                        }
                        //response.getData().setIsCertificate(1);
                        showCertificateDialog(response.getData().getIsCertificate());
                    } else if (response.getStatusCode() != Constant.CODE_OFFLINE) {
                        showBindToast(response.getErrorInfo());
                    }
                }, Throwable::printStackTrace));

    }

    private void showCertificateDialog(int isCertificate) {
        if (isCertificate > 0) {
            new AlertDialog.Builder(getContext()).setTitle("提示")
                    .setMessage("恭喜您完成本次培训，获得培训证书！")
                    .setPositiveButton("去看看", (dialog, which) -> {
                        MyCertificateActivity.actionStart(getContext());
                        dialog.dismiss();
                    })
                    .setNegativeButton("关闭", null)
                    .create().show();
        }
    }

    public static String splittime(String time){
        String[] times =  time.split(" ");
        return times[0];
    }

    @Override
    protected int getTitleStringRes() {
        return R.string.my_train_details;
    }

    private static class TrainListFragmentAdapter extends FragmentStateAdapter {

        private Fragment[] mFragments;

        TrainListFragmentAdapter(@NonNull FragmentActivity fragmentActivity, Fragment[] fragments) {
            super(fragmentActivity);
            mFragments = fragments;
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            return mFragments[position];
        }

        @Override
        public int getItemCount() {
            return mFragments.length;
        }
    }
}
